package com.hui.service.impl;

import com.hui.bean.UserBean;
import com.hui.service.IUserService;
import com.hui.dao.impl.UserDaoImpl;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 用户业务层实现类
 *
 */
public class UserServiceImpl implements IUserService {
    private static  UserDaoImpl userDao=null;

    static {

        try {
            userDao = UserDaoImpl.class.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }



    @Override
    public boolean userLogin(String username, String password) {
        if (StringUtils.isNotEmpty(username)&& StringUtils.isNotEmpty(password)) {
//            IUserDao userDao = new UserDaoImpl();
//            try {
//                UserBean user = userDao.userLogin(username, password);
//                if (user!=null){
//                    return true;
//                }
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }

            try {
                Method userLoginMethod = userDao.getClass().getMethod("userLogin", String.class, String.class);
               UserBean user=(UserBean) userLoginMethod.invoke(userDao, username, password);
               return user!=null;
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
